package avicit.bdp.dds.server.master.dispatch.host;

import avicit.bdp.dds.server.master.config.MasterConfig;
import avicit.bdp.dds.server.master.dispatch.host.assign.HostSelector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * host manager config
 */
@Configuration
public class HostManagerConfig {

    private AutowireCapableBeanFactory beanFactory;

    @Autowired
    private MasterConfig masterConfig;

    @Autowired
    public HostManagerConfig(AutowireCapableBeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    @Bean
    public HostManager hostManager() {
        String hostSelector = masterConfig.getHostSelector();
        HostSelector selector = HostSelector.of(hostSelector);
        HostManager hostManager;
        switch (selector){
            case RANDOM:
                hostManager = new RandomHostManager();
                break;
            case ROUNDROBIN:
                hostManager = new RoundRobinHostManager();
                break;
            case LOWERWEIGHT:
                hostManager = new LowerWeightHostManager();
                break;
            default:
                throw new IllegalArgumentException("unSupport selector " + hostSelector);
        }
        beanFactory.autowireBean(hostManager);
        return hostManager;
    }
}
